package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mentaleye.bean.TimeTableClassBean;
import com.mentaleye.biz.TimeTableBiz;
import com.mentaleye.biz.TimeTableClassBiz;
import com.mentaleye.dal.model.TimeTableClassExample;
import com.mentaleye.dal.model.TimeTableClassKey;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.OaClassBean;
import com.mentaleye.biz.OaClassBiz;
import com.mentaleye.dal.model.OaClassExample;
import com.mentaleye.service.OaClassService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Service
public class OaClassBizImpl implements OaClassBiz{
	
	private static Logger logger = Logger.getLogger(OaClassBizImpl.class);
	
	@Autowired private OaClassService oaClassService;
	@Autowired private TimeTableBiz timeTableBiz;
	@Autowired private TimeTableClassBiz timeTableClassBiz;

	@Override
	public CommonResult oaClassEdit(OaClassBean oaClassBean) {
		CommonResult result = null;
		try {
			result = new CommonResult();
			boolean success = true;
			boolean add = oaClassBean.getOaclassId() == null;
			if(add){
				success = oaClassService.oaClassAdd(oaClassBean);
			}else{
				success =  oaClassService.oaClassUpdate(oaClassBean);
			}
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("编辑班级异常",e);
		}
		return result;
	}

	@Override
	public CommonResult oaClassDrop(List<Integer> ids) {
		try {
			boolean result = true;
			OaClassBean oaClassBean = new OaClassBean();
			oaClassBean.setOaclassState(0);
			result = oaClassService.oaClassUpdateList(ids, oaClassBean);
			if(result){
				CommonResult rt = timeTableBiz.removeByClassIdList(ids);
				if(rt.getIsSuccess()){
					TimeTableClassExample example = new TimeTableClassExample();
					example.createCriteria().andOaClassIdIn(ids);
					List<TimeTableClassBean> timeTableClass = timeTableClassBiz.queryTimeTableClassByExample(example);
					List<TimeTableClassKey> list = new ArrayList<TimeTableClassKey>();
					for (TimeTableClassBean bean: timeTableClass) {
						TimeTableClassKey key = new TimeTableClassKey();
						key.setOaClassId(bean.getOaClassId());
						key.setTimeTableId(bean.getTimeTableId());
						list.add(key);
					}
				 	timeTableClassBiz.removeTimeTableClassList(list);
				}
			}
			return new CommonResult(result);
		} catch (Exception e) {
			logger.error("删除班级及课表异常",e);
		}
		return null;
	}

	@Override
	public void queryOaclassList(CommonPage commonPage) {
			oaClassService.queryOaclassList(commonPage);
	}

	@Override
	public OaClassBean queryOaclassData(int oaclassId, String oaclassName) {
		try {
			return oaClassService.queryOaclassData(oaclassId, oaclassName);
		} catch (Exception e) {
			logger.error("查询班级异常",e);
		}
		return null;
	}

	@Override
	public OaClassBean queryOaclassData(OaClassBean oaClassBean) {
		try {
			if(oaClassBean != null){
				Integer oaclassId = oaClassBean.getOaclassId();
				String oaclassName = oaClassBean.getOaclassName();
				return oaClassService.queryOaclassData(oaclassId, oaclassName);
			}
		} catch (Exception e) {
			logger.error("查询班级异常",e);
		}
		return null;
	}

	@Override
	public List<OaClassBean> queryOaclass(OaClassExample example) {
		try {
			return oaClassService.queryOaclass(example);
		} catch (Exception e) {
			logger.error("查询班级异常",e);
		}
		return null;
	}
	
	public OaClassBean queryOaclassById(Integer oaclassId){
		try {
			return oaClassService.queryOaclassById(oaclassId);
		} catch (Exception e) {
			logger.error("查询班级异常",e);
		}
		return null;
	}

	@Override
	public List<OaClassBean> getAllOaClass() {
		try {
			return oaClassService.queryAllOaClass();
		} catch (Exception e) {
			logger.error("查询所有班级异常",e);
			return null;
		}
	}

	@Override
	public void getListByPage(CommonPage page) {
		try {
			page.setPageData(this.oaClassService.getListByMap(page.pageToMap()));
		} catch (Exception e) {
			logger.error("查询班级异常",e);
			
		}
	}
	
	@Override
	public void toChangeClassPage(Map<String, Object> paramMap) {
		Integer classId=(Integer) paramMap.get("newClassId");
		try {
			OaClassBean bean = this.oaClassService.queryOaclassById(classId);
			paramMap.put("newClassName", bean.getOaclassName());
		} catch (Exception e) {
			logger.error("查询班级异常",e);
		}
	}

	@Override
	public CommonResult getCountByName(String className) {
		CommonResult result=new CommonResult();
		try {
			if(this.oaClassService.getCountByName(className)>0)
			{
				result.setIsSuccess(true);
			}
		} catch (Exception e) {
			logger.error("查询班级异常",e);
			result.setResultMsg("查询班级异常");
		}
		return result;
	}

}
